package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.*;
import java.util.ArrayList;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 
    boolean returnBack;
    
    /**
     * the constructor for the class
     */
    public DvdUI()
    {
        dvdCtr = new DVDCtr();
    }
    /**
     * starts the dvd menu
     */
    public void start(DVDCtr ctr)
    {
        dvdMenu();
    }
    
    /**
     * handles the menu and the controls
     */
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise =   writeDVDMenu();
            if (choise == 1)
            {
                String title = inputTitle();
            DVD dvd = dvdCtr.findDVD(title);
            System.out.print("\f");
            System.out.println("Title        : " + dvd.getTitle());
            System.out.println("Publisher    : " + dvd.getPublisher());
            System.out.println("Release Year : " + dvd.getReleaseYear());
            int noOfCopies = dvd.getNoOfCopies();
            if(noOfCopies > 0)
            {
                System.out.println("Copies       : " + noOfCopies);
            }
            else
            {
                System.out.println("No copies added yet.");
            }
            
            // Pause console loop to allow user to read output before returning to menus.
            returnBack = false;
            while(!returnBack)
            {
                pressKey();
            }
        }
            
            else
                {
                if(choise == 2){
                   createDVD();
                }
                else
                {
                    if(choise == 3)
                    {
                    String title = inputTitle();
                    DVD dvd = dvdCtr.findDVD(title);
                    
                    //Get input for Copy and send it to DVD
                    String serialNo = inputSerialNo();
                    int purchaseDate = purchaseDate();
                    double purchasePrice = purchasePrice();
                    
                    dvd.createCopy(serialNo, purchaseDate, purchasePrice);
                    }
                    else
                    {
                        if(choise == 4)
                        {
                             String title = inputTitle();
                             dvdCtr.deleteDVD(title);
                        }
                        else
                        {
                            if(choise == 5)
                            {
                                
                            }
                            else
                            {
                                exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    
    /**
     * prints out the menu
     */
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Create DVD description");
            System.out.println(" (3) Create DVD specimen");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choise: ");
            
            int choise = keyboard.nextInt();
            return choise;
    }
    /**
     * this methods makes it possible for the user to control
     * when to proceed in the UI
     */
    public void pressKey()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println("\nPress any key to continue:  ");
         keyboard.nextLine();
         returnBack = true;
    }
    
    /**
     * makes it possible for the user to type in information about DVD's
     * @returns DVDno, id, title, copyNo, serialNo, publisher, 
     * releaseYear, purchaseDate, purchasePrice
     */
    private int inputDVDNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD number: ");
        int no = keyboard.nextInt();
        return no;
    }
    
    private int inputID()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("type DVD ID:    ");
        int ID = keyboard.nextInt();
        return ID;
    }
        
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
    private int inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the copy number: ");
        int no = keyboard.nextInt();
        return no;
    }
    
    private String inputPublisher()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type publisher: " );
        String publisher = keyboard.nextLine();
        return publisher;
    }
    private String inputReleaseYear()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type year of release: " );
        String releaseYear = keyboard.nextLine();
        return releaseYear;
    }
    private String inputSerialNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type serial number: ");
        String serialNo = keyboard.nextLine();
        return serialNo;
    }
    private int purchaseDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type purchase date: ");
        int purchaseDate = keyboard.nextInt();
        return purchaseDate;
    }
    private double purchasePrice()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type purchase price: ");
        double purchasePrice = keyboard.nextDouble();
        return purchasePrice;
    }
    
   
    
    //there is need for more methods to read information about DVDs
    //and methods to display the information
    /**
     * makes it possible to search for DVD's
     */
    private void findDVD()
    {     
        String title = inputTitle();
        //is to be changed when the DVD controller is completed
        dvdCtr.findDVD(title);
    }
    
    /**
     * makes it possible to create a DVD from the UI  
     */
    private void createDVD()
    {
        int ID = inputID();
        String title = inputTitle();
        String publisher = inputPublisher();
        String releaseYear = inputReleaseYear();
        DVDCtr.createDVD(ID, title, publisher, releaseYear);
    }
    
    /**
     * makees it possible to create a copy in the UI
     */
    private void createCopy()
    {
        String serialNo = inputSerialNo();
        int purchaseDate = inputPurchaseDate();
        double purchasePrice = inputPurchasePrice();
        DVDCtr.createCopy(serialNo, purchaseDate, purchasePrice);
    }
    
//  to be coded later.
    private void deleteDVD()
    {
    }
    private void listAllDvds()
    {
    }
}